<template>
    <div class="manage">
        <!-- 点击添加弹框区域 -->
        <el-dialog :title="modelType === 0 ? '添加用户' : '编辑用户'" :visible.sync="dialogVisible" width="50%"
            @click="handleClose">
            <el-form :model="form" label-width="80px" :inline="true" :rules="rules" ref="form">
                <el-form-item label="姓名" prop="name">
                    <el-input placeholder="请输入姓名" v-model="form.name"></el-input>
                </el-form-item>
                <el-form-item label="年龄" prop="age">
                    <el-input placeholder="请输入年龄" v-model.number="form.age"></el-input>
                </el-form-item>
                <!-- 添加性别 -->
                <el-form-item label="性别" prop="sex">
                    <el-select v-model="form.sex" placeholder="请选择">
                        <el-option label="男" :value="1"></el-option>
                        <el-option label="女" :value="0"></el-option>
                    </el-select>
                </el-form-item>
                <el-form-item label="出生日期" prop="birth">
                    <el-date-picker v-model="form.birth" type="date" placeholder="选择日期"
                        value-format="yyyy-MM-dd"></el-date-picker>
                </el-form-item>
                <el-form-item label="地址" prop="addr">
                    <el-input v-model="form.addr" placeholder="请输入地址"></el-input>
                </el-form-item>
            </el-form>
            <!-- 底部 -->
            <span slot="footer" class="dialog-footer" style="justify-content: flex-end;">
                <el-button @click="handleCancel">取 消</el-button>
                <el-button type="primary" @click="submit">确 定</el-button>
            </span>
        </el-dialog>
        <!-- 添加用户按钮 -->
        <!-- 头部添加用户按钮 -->
        <div class="manage-header">
            <!-- 添加用户按钮 -->
            <el-button type="primary" @click="handleAdd">+添加</el-button>
            <!-- form的搜索区域 -->
            <el-form :inline="true" :model="userForm">
                <el-form-item>
                    <el-input placeholder="请输入名称" v-model="userForm.name"></el-input>
                </el-form-item>
                <!-- 提交区域 -->
                <el-form-item>
                    <el-button type="primary" @click="onsubmit">查询</el-button>
                </el-form-item>
            </el-form>
        </div>
        <div class="common-table">
            <!-- 表格区域 -->
            <el-table stripe height="90%" :data="tableData" style="width: 100%;">
                <!-- 用户数据展示区域 -->
                <el-table-column prop="name" label="姓名"></el-table-column>
                <el-table-column prop="age" label="年龄"></el-table-column>
                <el-table-column prop="sex" label="性别">
                    <!-- 处理性别 -->
                    <template slot-scope="scoped">
                        <span style="margin-left:">{{ scoped.row.sex === 0 ? '女' : '男' }}</span>
                    </template>
                </el-table-column>
                <el-table-column prop="birth" label="出生日期"></el-table-column>
                <el-table-column prop="addr" label="地址"></el-table-column>
                <el-table-column label="操作">
                    <template slot-scope="scoped">
                        <el-button size="mini" @click="handleEdit(scoped.row)">编辑</el-button>
                        <el-button size="mini" type="danger" @click="handleDelete(scoped.row)">删除</el-button>
                    </template>
                </el-table-column>
            </el-table>
            <!-- 分页组件 -->
            <div class="pager">
                <!-- current-change切换页码 -->
                <el-pagination @current-change="handlePage" layout="prev, pager, next" :total="total">
                </el-pagination>
            </div>
        </div>
    </div>

</template>
<script>
import { getUser, addUser, editUser, delUser } from '../api'
export default {
    name: 'UserPage',
    data() {
        return {
            dialogVisible: false,
            form: {
                name: '',
                age: '',
                sex: '',
                birth: null,
                addr: ''
            },
            rules: {
                name: [
                    { required: true, message: '请输入姓名' }
                ],
                age: [
                    { required: true, message: '请输入年龄' }
                ],
                sex: [
                    { required: true, message: '请选择性别' }
                ],
                birth: [
                    { required: true, message: '请选择出生日期' }
                ],
                addr: [
                    { required: true, message: '请输入地址' }
                ]
            },

            tableData: [],
            modelType: 0,//0表示新增，1表示编辑

            total: 0,//当前的总条数
            pageData: {
                page: 1,
                limit: 10
            },
            userForm: {
                name: '',
            }
        }
    },
    methods: {
        submit() {
            this.$refs.form.validate((valid) => {
                // 能否通过校验，后续对表单数据的处理
                if (valid) {
                    // 提交表单
                    console.log('提交的用户信息', this.form);
                    if (this.modelType === 0) {
                        addUser(this.form).then(res => {
                            this.getList()
                        })
                    } else {
                        editUser(this.form).then(res => {
                            this.getList()
                        })
                    }
                    //清空表达数据
                    this.$refs.form.resetFields();
                    this.dialogVisible = false;
                } else {
                    console.error('表单验证失败');
                    return false;
                }
            });
        },
        // 右下角取消按钮
        handleCancel() {
            this.handleClose();
        },
        // 右上角关闭按钮
        handleClose() {
            // 这里可以添加额外的逻辑
            this.$refs.form.resetFields();
            this.dialogVisible = false;
        },
        //编辑
        handleEdit(row) {
            this.modelType = 1
            console.log('点击编辑');
            this.dialogVisible = true;
            // 需要利用深拷贝的方法
            this.form = JSON.parse(JSON.stringify(row));
        },
        //删除
        handleDelete(row) {
            console.log('点击删除');
            //删除弹窗
            this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
            }).then(() => {
                //传递对象
                delUser({ id: row.id }).then(() => {
                    this.$message({
                        type: 'success',
                        message: '删除成功!'
                    });
                    this.getList()
                })

            }).catch(() => {
                this.$message({
                    type: 'info',
                    message: '已取消删除'
                });
            });
        },
        //新增
        getList() {
            // 模拟异步获取数据
            getUser({ params: { ...this.userForm, ...this.pageData } }).then(res => {
                console.log(res);
                this.tableData = res.data.data.list;
                console.log(this.tableData);
                this.total = res.data.data.count || 0;
            })

        },
        handleAdd() {
            console.log('点击新增');
            this.modelType = 0
            this.dialogVisible = true;
        },
        //handlePage 选择页码的回调函数
        handlePage(val) {
            console.log(val);
            this.pageData.page = val;
            // 重新获取数据
            this.getList()
        },
        // 列表查询
        onsubmit() {
            this.getList()
        }

    },
    mounted() {
        this.getList()
    }
}
</script>
<style lang="less" scoped>
.manage {
    height: 90%;

    /* 确保 .manage 有一个明确的高度 */
    .manage-header {
        display: flex;
        justify-content: space-between;
        align-items: center;
    }

    .common-table {
        position: relative;
        height: calc(100% - 62px);
        // height: 80%;

        /* 基于 .manage 的高度计算 */
        .pager {
            position: absolute;
            bottom: 0;
            right: 20px;
        }
    }
}
</style>